﻿Imports Q7P1TTS.Lib
Imports System.Text.RegularExpressions
Imports ComponentFactory.Krypton.Toolkit

Public Class EmployeeForm
    Dim emp As Employee

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        emp = New Employee()
    End Sub

    Public Sub New(ByVal ID As Integer)
        InitializeComponent()
        emp = EmployeeController.load(ID)
    End Sub
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        If validateData() Then
            emp.FirstName = txtFirstName.Text.Trim()
            emp.LastName = txtLastName.Text.Trim()
            emp.DOB = ConvertHelper.ToUnixTimestamp(dpDOB.Value.ToLocalTime())
            emp.JoinDate = ConvertHelper.ToUnixTimestamp(dpJoinDate.Value.ToLocalTime())
            emp.Designation = cboDesignationSelector.SelectedItem.ToString()
            emp.DeptID = cboDeptSelector.SelectedValue
            emp.Email = txtEmail.Text.Trim()
            If emp.Password <> txtPassword.Text.Trim() Then
                emp.Password = ConvertHelper.ToMD5(txtPassword.Text.Trim())
            End If
            If String.IsNullOrEmpty(emp.Account) Then
                emp.Account = ConvertHelper.GenerateAccount("EMP")
            End If
            emp.Address = txtAddress.Text.Trim()
            emp.City = txtCity.Text.Trim()
            emp.State = txtState.Text.Trim()
            emp.Zip = txtZip.Text.Trim()
            emp.Phone = txtPhone.Text.Trim()
            emp.LicenseNumber = txtLicenseNum.Text.Trim()
            If EmployeeController.save(emp) > 0 Then
                'go to list of employee
                Dim mainForm As MainForm = Me.FindForm()
                mainForm.GoToForm(New EmployeeList())
            Else
                MessageBox.Show("Sorry! There was an error while generating account. Please try again later.", "Updated Failed!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If
    End Sub
    Private Function validateData() As Boolean
        Dim res As Boolean = True
        ' validate empty data
        If String.IsNullOrEmpty(txtEmail.Text.Trim()) Then
            errorProvider.SetError(txtEmail, "Email can not be left blank!")
            res = res And False
        Else
            errorProvider.SetError(txtEmail, "")
        End If
        If String.IsNullOrEmpty(txtPassword.Text.Trim()) Then
            errorProvider.SetError(txtPassword, "Password can not be left blank!")
            res = res And False
        Else
            errorProvider.SetError(txtPassword, "")
        End If
        If String.IsNullOrEmpty(txtFirstName.Text.Trim()) Then
            errorProvider.SetError(txtFirstName, "First Name can not be left blank!")
            res = res And False
        Else
            errorProvider.SetError(txtFirstName, "")
        End If
        If String.IsNullOrEmpty(txtLastName.Text.Trim()) Then
            errorProvider.SetError(txtLastName, "Last Name can not be left blank!")
            res = res And False
        Else
            errorProvider.SetError(txtLastName, "")
        End If
        If String.IsNullOrEmpty(txtPhone.Text.Trim()) Then
            errorProvider.SetError(txtPhone, "Phone can not be left blank!")
            res = res And False
        Else
            errorProvider.SetError(txtPhone, "")
        End If
        If cboDeptSelector.SelectedValue <= 0 Then
            errorProvider.SetError(cboDeptSelector, "Department can not be left blank!")
            res = res And False
        Else
            errorProvider.SetError(cboDeptSelector, "")
        End If
        ' validate email
        Dim r As New Regex("^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$")
        If Not r.IsMatch(txtEmail.Text.Trim()) Then
            errorProvider.SetError(txtEmail, "Email is not valid!")
            res = res And False
        Else
            errorProvider.SetError(txtEmail, "")
        End If
        ' validate phone
        r = New Regex("^[2-9]\d{2}-\d{3}-\d{4}$")
        If Not String.IsNullOrEmpty(txtPhone.Text.Trim()) And Not r.IsMatch(txtPhone.Text.Trim()) Then
            errorProvider.SetError(txtPhone, "Phone is not valid!")
            res = res And False
        Else
            errorProvider.SetError(txtPhone, "")
        End If
        ' validate zip
        r = New Regex("^\d{5,6}")
        If Not String.IsNullOrEmpty(txtZip.Text.Trim()) And Not r.IsMatch(txtZip.Text.Trim()) Then
            errorProvider.SetError(txtZip, "Zip is not valid!")
            res = res And False
        Else
            errorProvider.SetError(txtZip, "")
        End If
        Return res
    End Function

    Private Sub EmployeeForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cboDesignationSelector.SelectedIndex = 0
        cboDeptSelector.DataSource = DepartmentController.load("Select...", 0)
        Dim mainForm As MainForm = Me.FindForm()
        mainForm.AcceptButton = Me.btnAdd
        mainForm.CancelButton = Me.btnCancel
        If emp.ID > 0 Then
            
            mainForm.SetHeader("Update Employee: " & emp.FirstName & " " & emp.LastName)
            Try
                txtEmail.Text = emp.Email
                txtPassword.Text = emp.Password
                txtFirstName.Text = emp.FirstName
                txtLastName.Text = emp.LastName
                txtAddress.Text = emp.Address
                txtCity.Text = emp.City
                txtState.Text = emp.State
                txtZip.Text = emp.Zip
                txtPhone.Text = emp.Phone
                cboDeptSelector.SelectedIndex = cboDeptSelector.Items.IndexOf(emp.Department)
                cboDesignationSelector.SelectedIndex = cboDesignationSelector.Items.IndexOf(emp.Designation)
                dpDOB.Value = ConvertHelper.ToDateTime(emp.DOB)
                dpJoinDate.Value = ConvertHelper.ToDateTime(emp.JoinDate)
                txtLicenseNum.Text = emp.LicenseNumber
            Catch ex As Exception

            End Try
        Else
            mainForm.SetHeader("Add New Employee")
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Dim mainForm As MainForm = Me.FindForm()
        mainForm.GoToForm(New EmployeeList(), "Employee List")
    End Sub

    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        For Each c As Control In KryptonPanel1.Controls
            If TypeOf c Is KryptonTextBox Then
                Dim txt As KryptonTextBox = c
                txt.Text = ""
            End If
            If TypeOf c Is KryptonRichTextBox Then
                Dim txt As KryptonRichTextBox = c
                txt.Text = ""
            End If
            If TypeOf c Is KryptonComboBox Then
                Dim cbo As KryptonComboBox = c
                cbo.SelectedIndex = 0
            End If
        Next
    End Sub
End Class
